package com.czy.owner.ui.hotshop;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.czy.owner.R;
import com.czy.owner.adapter.HomeServiceMoreAdapter;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.CarInfo;
import com.czy.owner.entity.ShoppingModel;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.garage.PickBrandActivity;
import com.czy.owner.ui.login.LoginActivity;
import com.czy.owner.ui.work.SetMileageActivity;
import com.czy.owner.ui.work.WorkShowActivity;
import com.czy.owner.utils.PhoneUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.czy.owner.ui.work.WorkShowActivity.TRANSMISSION_WORK_EXTEND_KEY;
import static com.czy.owner.ui.work.WorkShowActivity.TRANSMISSION_WORK_TYPE_KEY;
import static com.czy.owner.ui.work.WorkShowActivity.WorkJumpType.INDEX;

public class HomeServiceMoreActivity extends BaseActivity {

    @BindView(R.id.easy_recycleview)
    EasyRecyclerView easyRecyclerView;
    private final static int REQUEST_CODE_ADD_CAR = 1;
    private final static int REQUEST_CODE_ADD_MILEAGE = 99;
    String modelsId;
    private boolean needRefreshCar = false;
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_home_service_more;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.more_service);
        List<ShoppingModel.ModelsListBean> homeServiceList = (List<ShoppingModel.ModelsListBean>) getIntent().getSerializableExtra("list");
        easyRecyclerView.setLayoutManager(new GridLayoutManager(HomeServiceMoreActivity.this, 4));
        HomeServiceMoreAdapter adapter = new HomeServiceMoreAdapter(HomeServiceMoreActivity.this);
        easyRecyclerView.setAdapter(adapter);
        adapter.addAll(homeServiceList);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<ShoppingModel.ModelsListBean>() {
            @Override
            public void onItemClick(View view, int position, ShoppingModel.ModelsListBean o) {
                 modelsId = o.getModelsId() + "";
                CarInfo mCarInfo = UserHelper.getInstance().getDefaultCar();
                if (UserHelper.getInstance().isLogined()) {
                    if (UserHelper.getInstance().getDefaultCarIndex() == 0 && mCarInfo == null) {
                        Toast.makeText(HomeServiceMoreActivity.this, "数据加载中,请稍候!", Toast.LENGTH_SHORT).show();
                    } else if (UserHelper.getInstance().getDefaultCarIndex() == -1) {
                        Intent intent = new Intent();
                        intent.setClass(HomeServiceMoreActivity.this, PickBrandActivity.class);
                        intent.putExtra(PickBrandActivity.START_FLAG, 0);
                        startActivityForResult(intent, REQUEST_CODE_ADD_CAR);
                    } else if (mCarInfo.getCurrentMileage() == 0) {
                        startActivityForResult(new Intent(HomeServiceMoreActivity.this, SetMileageActivity.class).putExtra(Constants.TRANSMISSION_CONTENT_KEY, mCarInfo), REQUEST_CODE_ADD_MILEAGE);
                    } else {
                        startActivity(new Intent(HomeServiceMoreActivity.this, WorkShowActivity.class).putExtra(Constants.TRANSMISSION_CONTENT_KEY, mCarInfo).putExtra(TRANSMISSION_WORK_TYPE_KEY, INDEX).putExtra(TRANSMISSION_WORK_EXTEND_KEY, modelsId));
                    }
                } else {
                   startActivity(new Intent(HomeServiceMoreActivity.this, LoginActivity.class).putExtra(Constants.TRANSMISSION_CONTENT_KEY, false));
                }
                }

            }
        );

    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ADD_CAR && resultCode == Constants.ACTIVITY_RESULT_CODE_CAR_ADD) {
            // TODO 车辆添加成功返回
            needRefreshCar = false;
            getMyGarage();
        } else if (requestCode == REQUEST_CODE_ADD_MILEAGE && resultCode == RESULT_OK) {
            // 当前公里数 返回
            needRefreshCar = false;
            startActivity(new Intent(HomeServiceMoreActivity.this, WorkShowActivity.class).putExtra(Constants.TRANSMISSION_CONTENT_KEY, UserHelper.getInstance().getDefaultCar()).putExtra(TRANSMISSION_WORK_TYPE_KEY, INDEX).putExtra(TRANSMISSION_WORK_EXTEND_KEY, modelsId));

        }
    }
    private void getMyGarage() {
        RequestParams params = new RequestParams(Constants.APP_API_BASE + Constants.OWN_CARS);
        params.addBodyParameter("session", UserHelper.getInstance().getSession(this));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                String data = PhoneUtils.checkResponseFlag(HomeServiceMoreActivity.this, result);
                if (data != null) {
                    List<CarInfo> dataList = new ArrayList<>();
                    try {
                        Gson gson = new GsonBuilder().create();
                        dataList = gson.fromJson(data, new TypeToken<List<CarInfo>>() {
                        }.getType());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    UserHelper.getInstance().setCarInfoList(dataList);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
